<template>
  <div class='container' id="projectPopup">
       <el-form :model="formData" ref="formData" label-width="150px">
           <el-row>
                <el-col :span="8">
                  <el-form-item label="合同编码" prop="reAmountCause">
                    <el-input placeholder="请输入内容" v-model="formData.contractcode">
                    </el-input>
                </el-form-item>
               </el-col>
               <el-col :span="8">
                   <el-button @click="queryHandel">查询</el-button>
               </el-col>
           </el-row>
      </el-form>
       <DicTable
            border
            :header-cell-style="{ 'text-align': 'center' }"
            :cell-style="{ 'text-align': 'center' }"
            ref="commonRegionTable"
            stripe
             max-height="500"
             highlight-current-row
             :columnData="proContractColumns"
             :tableData="proContractTableData"
             :loading="proContractTableLoading"
             :currentPage="proContractTablePage.pageIndex"
             :pageSize="proContractTablePage.pageSize"
             :pager="true"
            :totalResult="proContractTablePage.rowCount"
             @cell-click="proContractCurrentChangeEvent"
             @on-page-change="proContractHandlePageChange"
        >
              <template #ifictproject="{ row }">
              <span  v-if="row.ifictproject=='1'">是</span>
              <span  v-else-if="row.ifictproject=='0'">否</span>
              </template>
              <template #relationitemflag="{ row }">
                 <span  v-if="row.relationitemflag=='1'">是</span>
                 <span  v-else-if="row.relationitemflag=='0'">否</span>
              </template>
        </DicTable>
  </div>
</template>

<script>
import DicTable from "@/components/public/DicTable";
import OrderService from "@/api/order/OrderService.js";
  export default {
      props: {
          CTOInfo: {
              type: Object,
          }
      },
    components: {
      DicTable
    },
    // 这里存放数据
    data () {
      return {
        formData:{
          contractcode:""
        },
         selected:{
            name:"合同编码",
            staffId:"",
            staffName:""
          },
      proContractColumns: [
        { field: "contractcode", title: "合同编码",width:200 },
        { field: "contractname", title: "合同名称" ,width:200},
        { field: "contracttype", title: "合同编号类型",width:160 },
        { field: "signenddate", title: "签约日期",width:200 },
        { field: "signdate", title: "合同开始日期",width:200 },
        { field: "enddate", title: "截止日期",width:200 },
        { slotName: "ifictproject", title: "是否ICT合同",width:100 },
        { slotName: "relationitemflag", title: "是否关联项目",width:100 },
        { field: "itemcode", title: "项目编码",width:200 },
        { field: "itemname", title: "项目名称",width:200 },
        { field: "itemmanager", title: "项目经理",width:200 },
        // { field: "REGION_NAME", title: "有效性",width:200},
        // { field: "REGION_NAME", title: "来源",width:90 },
      ],
      proContractTableData: [],
      proContractTableLoading: false,
      proContractTablePage: {
        pageIndex: 1,
        pageSize: 5,
        rowCount: 0,
        pageCount: 0,
      },
      }
    },
    // 生命周期 - 创建完成（可以访问当前this实例）
    created () {},
    mounted () {
      this.loadContactCdOptions();
    },
    // 方法集合
    methods: {
        async proContractCurrentChangeEvent(){
          let val = arguments[0].row;
          
          
          // 
          // 
          // 
          /*
          *    规则:1.所有合同默认全部有效
          */
          /*let getProdOffer_by_custSoNumber1  = {
              interFaceType:"ORDER",
              sqlId:"2021070106", //2021070105  -COUNT0返回为 0
              paramStr: "custSoNumber:"+this.CTOInfo.custSoNumber+";custId:"+this.CTOInfo.custId ,
          }
          let getProdOffer = await OrderService.executeSql(getProdOffer_by_custSoNumber1);
            if(getProdOffer.code == 200 && "resList" in getProdOffer.meta) {
                
                let countnum = getProdOffer.meta.resList.executeSqlResps[0].COUNT0 || 0;
                let clickData = val;
                let zflag = "";
                if (countnum > 0) {
                    
                    if (clickData.source != "MSS域") {
                        this.$alert("该合同不是M域合同，请重新选择!", "提示", { type: "warning" });
                        return;
                    }
                    if (clickData.ifictproject != "1") {
                        this.$alert("该合同不是ICT合同，请重新选择!", "提示", { type: "warning" });
                        return;
                    }
                    let qryIsIDCProduct = {
                        interFaceType: "ORDER",
                        sqlId: "2021070107",
                        paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                    }
                    let IDCFlag = await OrderService.executeSql(qryIsIDCProduct);
                    let IDCFlagN = IDCFlag.meta.resList.executeSqlResps[0].COUNT0 || "";
                    let qryIsYunDiProduct = {
                        interFaceType: "ORDER",
                        sqlId: "2021070109",
                        paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                    }
                    let YundiFlag = await OrderService.executeSql(qryIsYunDiProduct);
                    let YundiFlagN = YundiFlag.meta.resList.executeSqlResps[0].COUNT0 || "";
                    if ( IDCFlagN <= 0 && YundiFlagN <= 0) { // 校验登录人是否配置“合同立项校验”
                        if ( clickData .relationitemflag == "否") { // 校验组织是否配置“合同立项校验”
                            this.$alert("该合同未关联立项完成的项目，请重新选择!", "提示", { type: "warning" });
                            return;
                        }
                    }
                    zflag = '1';
                } else {
                    zflag = '1';
                }
                if (zflag == '1') {

                    //var lhflag = $('#laihan_flag').val();

                    if (clickData.contractcode == 'JXXYJTYHTBH000SLD' /!*&& lhflag == 0*!/) {
                        this.$alert("ICT业务、网管专家、云堤、医疗影像云为客户来函时，才允许选择三联单合同！", "提示", { type: "warning" });
                        return;
                    }
                    let qryFreeCircuitCntByCSN = {
                        interFaceType: "ORDER",
                        sqlId: "2021070114",
                        paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                    }
                    let freeflag = await OrderService.executeSql(qryFreeCircuitCntByCSN);
                    let freeflagN = freeflag.meta.resList.executeSqlResps[0].COUNT0 || "";
                    if (clickData.contractcode == 'JXXYJTYHTBH000GMY' && freeflagN <= 0) {
                        this.$alert("只有公免电路或工程电路才允许选择公免合同,请您重新选择！", "提示", { type: "warning" });
                        return;
                    }
                    if (clickData.contractcode == "JXXYJTYHTBH000GJY") {
                        let findOrderOrg = {
                            interFaceType: "ORDER",
                            sqlId: "2021070103",
                            paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                        }
                        let orgId = await OrderService.executeSql(findOrderOrg);
                        let orgIdN = orgId.meta.resList.executeSqlResps[0].COUNT0 || "";
                        let qryStaffDeptByCSN = {
                            interFaceType: "ORDER",
                            sqlId: "2021070113",
                            paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                        }
                        let staffDept = await OrderService.executeSql(qryStaffDeptByCSN);
                        let staffDeptN = staffDept.meta.resList.executeSqlResps[0].COUNT0 || "";
                        let qryNicheCnt = {
                            interFaceType: "ORDER",
                            sqlId: "2021070102",
                            paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                        }
                        let nicheCnt = await OrderService.executeSql(qryNicheCnt);
                        let nicheCntN = nicheCnt.meta.resList.executeSqlResps[0].COUNT0 || "";
                        if (nicheCntN > 0 || ("1944889,1944890,1944891,1950850,1978013".indexOf(orgIdN) < 0 && staffDeptN != "1947362")) {
                            this.$alert("只有海外公司、集团公司(国际部)在CRM受理订单才可以选择国际三联单合同号【JXXYJTYHTBH000GJY】,请您重新选择！", "提示", { type: "warning" });
                            return;
                        }
                    }
                    if (clickData.contractcode == "JXXYJTYHTBH000CSY") {
                        let qryIsOtn = {
                            interFaceType: "ORDER",
                            sqlId: "2021070110",
                            paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                        }
                        let countOtn = await OrderService.executeSql(qryIsOtn);
                        let countOtnN = countOtn.meta.resList.executeSqlResps[0].COUNT0 || "";
                        if (countOtnN > 0) {
                            this.$alert("请选择合同类型非测试合同的数据", "提示", { type: "warning" });
                            return;
                        }
                    }
                    let qryIsZhuanXian = {
                        interFaceType: "ORDER",
                        sqlId: "2021070108",
                        paramStr: "custSoNumber:" + this.CTOInfo.custSoNumber + ";custId:" + this.CTOInfo.custId,
                    }
                    let zhuanXianFlag = await OrderService.executeSql(qryIsZhuanXian);
                    let zhuanXianFlagN = zhuanXianFlag.meta.resList.executeSqlResps[0].COUNT0 || "";
                    if (zhuanXianFlagN <= 0) { //不允许选C-26合同
                        this.$alert("本合同隶属网络安全服务合同，该产品不可以选择，请您重新选择正确的合同！", "提示", { type: "warning" });
                        return;
                    }
                } else {
                    this.$alert("response.meta.resDesc！", "提示", { type: "warning" });
                }
            }*/

          //原始代码
          this.selected.staffId = val.contractcode;
          this.selected.staffName = val.contractname;
          this.$emit("clickRow",this.selected);
      },
      proContractHandlePageChange(val){
         
      },
      // 合同编码-查询
      queryHandel(){
        this.loadContactCdOptions();
      },
       /**
     * 加载合同编码配置选项
     */
    async loadContactCdOptions() {
           let booleanType = this.formData.contractcode != "" ? 1 : 0 ;
           let obj = {
               requestObject: {
                   contractJson: {
                       contractcode: this.formData.contractcode,
                       contractname: "",
                       itemcode: "",
                       itemname: "",
                       type: booleanType,
                       page: {
                           pageNo: this.proContractTablePage.pageIndex,
                           pageSize: this.proContractTablePage.pageSize,
                       },
                   },
               },
           };
          /*const data = {
             contractcode: this.formData.contractcode,//合同编码
             pageSize: this.proContractTablePage.pageSize,
             pageIndex: this.proContractTablePage.pageIndex,
          };*/

          let res = await OrderService.qryContractList(obj);
          
          if (res.code == 200) {
            this.proContractTableData = res.meta;
          }
        },
    },
    // 监听属性 类似于data概念
    computed: {},
    // 监控data中的数据变化
    watch: {},
    // 如果页面有keep-alive缓存功能，这个函数会触发
    activated () {},
    // 生命周期 - 挂载完成（可以访问DOM元素）
    beforeCreate () {},
    // 生命周期 - 数据挂载之前
    beforeMount () {},
    // 生命周期 - 数据更新之前
    beforeUpdate() {},
    // 生命周期 - 数据更新之后
    updated () {},
    // 生命周期 - 页面销毁之前
    beforeDestroy() {},
    // 生命周期 - 页面销毁完成
    destroyed () {},
  }
</script>

<style scoped>
  /* @import url(); 引入公共css类 */

</style>
